#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.empty())return "";
        string first = strs[0];
        for (int i = 0; i < first.length(); i++) {
            char c = first[i];
            for (int j = 0; j < strs.size(); j++) {
                if (i >= strs[j].length() || strs[j][i] != c) {
                    return first.substr(0, i);
                }
            }
        }
        return first;
    }
};
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.empty())return "";
        int minlen = strs[0].length();
        for (string s : strs) {
            minlen = min((int)s.length(), minlen);
        }
        for (int i = 0; i < strs[0].length(); i++) {
            char c = strs[0][i];
            for (int j = 0; j < strs.size(); j++) {
                if (strs[j][i] != c) {
                    return strs[0].substr(0, i);
                }
            }
        }
        return strs[0];
    }
};
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.empty())return "";
        sort(strs.begin(), strs.end());
        string first = strs[0];
        string last = strs.back();
        int i = 0;
        while (first[i] == last[i] && i < first.length() && i < last.length()) { i++; }
        return first.substr(0, i);
    }
};